﻿/*
 * ControlButton Class
 * Administra todos os botões de opções (Radio e Check)
 *
 * @author 		Guilherme Almeida e Rodrigo Moyle
 * @version		1.0
 *
 * http://code.google.com/p/formup/
 *
 * //////////////////////////////////////////////////////////////////////////////
 */
class FlashForm.ControlButton {
	var grpName:String;
	var defaultValue:String;
	var arrayRadioCheck:Array;
	var local:MovieClip;
	var cType:String;
	var qtd:Number;
	var arrayItem:Array;
	//
	function ControlButton(grpName:String, defaultValue:String, local:MovieClip, cType:String) {
		this.grpName = grpName;
		this.defaultValue = defaultValue;
		this.arrayRadioCheck = defaultValue.split(',');
		this.local = local;
		this.cType = cType;
		this.qtd = arrayRadioCheck.length;
		this.arrayItem = new Array();
		this.init();
	}
	//
	public function init():Void {
		var self = this;
		this.local.createEmptyMovieClip(grpName, this.local.getNextHighestDepth());
		this.local[this.grpName].createTextField("field", this.local[this.grpName].getNextHighestDepth(), 0, 0, 100, 20);
		this.local[this.grpName]._visible = false;
		//
		this.local[this.grpName].field.watch('text', function (prop, oldVal, newVal) {
			oldVal = this[prop];
			return self.watchText.apply(self, [newVal, oldVal]);
		});
		//
		for (var i = 0; i<this.qtd; i++) {
			var clip = this.getClip(this.arrayRadioCheck[i]);
			clip.gotoAndStop(1);
			clip.fValue = this.arrayRadioCheck[i];
			//
			clip.onRelease = function() {
				if (self.isLabel(this, 'unchecked', true)) {
					self.markCheck(this.fValue);
				} else {
					self.uncheck(this.fValue);
				}
				this.onClick.apply(this);
			};
		}
	}
	//
	private function watchText(newVal, oldVal) {
		var itens:Array = newVal.split(',');
		if (this.cType == 'radio') {
			itens = itens.slice(-1);
		}
		for (var i in this.arrayRadioCheck) {
			i = this.arrayRadioCheck[i];
			this.animate(i, this.inArray(itens, i));
		}
		return itens.join(',');
	}
	//
	private function getClip(item:String):MovieClip {
		return this.local[this.grpName+'_'+item];
	}
	//
	private function animate(item:String, isCheck:Boolean):Void {
		if (isCheck) {
			var frame = 'unchecked';
			var stopFrame = 'checked';
		} else {
			var frame = 'checked';
			var stopFrame = 'unchecked';
		}
		var clip = this.getClip(item);
		if (this.isLabel(clip, stopFrame, true)) {
			return;
		}
		var self = this;
		clip.onEnterFrame = function() {
			if (self.isLabel(this, 'checked') || self.isLabel(this, 'unchecked')) {
				this.stop();
				delete this.onEnterFrame;
			}
		};
		clip.gotoAndPlay(frame);
	}
	//
	public function isLabel(clip:MovieClip, cLabel:String, action:Boolean):Boolean {
		var iniFrame = clip._currentframe;
		clip.gotoAndStop(cLabel);
		var returnVal = (clip._currentframe == iniFrame);
		if (!action) {
			clip.gotoAndPlay(iniFrame);
		} else {
			clip.gotoAndStop(iniFrame);
		}
		return returnVal;
	}
	//
	public function markCheck(item:String, clearCheck:Boolean):Void {
		if (clearCheck) {
			this.clearCheck();
		}
		var arrayCurrent:Array = this.local[this.grpName].field.text.split(',');
		arrayCurrent.push(item);
		arrayCurrent = this.arrayUnique(arrayCurrent);
		this.local[this.grpName].field.text = arrayCurrent.join(',');
	}
	//
	public function uncheck(item:String):Void {
		if (this.cType == 'radio') {
			return;
		}
		var arrayCurrent:Array = this.local[this.grpName].field.text.split(',');
		var arrayNew:Array = new Array();
		for (var i in arrayCurrent) {
			i = arrayCurrent[i];
			if (i != item) {
				arrayNew.push(i);
			}
		}
		this.local[this.grpName].field.text = arrayNew.join(',');
	}
	//
	public function clearCheck():Void {
		this.local[this.grpName].field.text = '';
	}
	/*
	* Funções de array
	*/
	public function inArray(arr:Array, val):Boolean {
		for (var i in arr) {
			if (val == arr[i]) {
				return true;
			}
		}
		return false;
	}
	//
	public function arrayUnique(arr:Array):Array {
		var objUnique:Object = new Object();
		for (var i in arr) {
			i = arr[i];
			objUnique[i] = i;
		}
		var arrUnique:Array = new Array();
		for (var i in objUnique) {
			arrUnique.push(i);
		}
		return arrUnique;
	}
}
